package com.zhuss.util;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zhuss.enums.MessageType;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;

@Service
public class AliSmsService {
	
	Logger logger = LoggerFactory.getLogger(AliSmsService.class);
	
	
//	private static final String accessKeyId = "LTAI4G8t8cPspYVRDFUYM77z";
	private static final String accessKeyId = "LTAI4G2v9ZqNSKewVzxiLUF4";
	
//	private static final String accessKeySecret = "cq1qbuEXQ4vlTE1PY1Jxac0wTIcICy";
	private static final String accessKeySecret = "pDKdwMVs6l1TbK9Pf6k3Sd1UlJtdvm";
	
//	private static final String signName = "定制出行";
	private static final String signName = "昆山长运定制";
	
	// private static final String loginTemplateCode = "SMS_192335611";
	
	// private static final String loginTemplateParam = "{\"code\":\"123321\"}";
	
	//替换成你的AccessKey 
    //final String accessKeyId ="XXXX";//你的accessKeyId,
    //final String accessKeySecret ="xxxxxx";//你的accessKeySecret
	
	/*public boolean sendLoginMsg(String phone) throws DzcxException {
		return sendMsg(phone, loginTemplateCode, loginTemplateParam);
	}*/
	
	public boolean sendLoginMsg(String phone, String loginTemplateCode, String loginTemplateParam) throws DzcxException {
		return sendMsg(phone, loginTemplateCode, loginTemplateParam);
	}
	
	public boolean sendOrderMsg(String phone, String templateParam) throws DzcxException {
		return sendMsg(phone, MessageType.购票.tag(), templateParam);
	}
	
	public boolean sendOrderMsg5(String phone, String templateParam) throws DzcxException {
		return sendMsg(phone, MessageType.购票5.tag(), templateParam);
	}
	
	public boolean sendTakeCarMsg(String phone, String templateParam) throws DzcxException {
		return sendMsg(phone, MessageType.乘车.tag(), templateParam);
	}

	/**
	 * 短信发送接口信息  支持批量发送 ps--目前签名信息仅设置一个
	 * @param phone        需要发送的电话号码，支持多个电话号码 格式为"13600000000,15000000000"
	 * @param templateCode  明确需要使用哪个模板，可以从阿里云控制台查看
	 * @param templateParam 模板内需要填充的字段及字段值 格式为("{\"name\":\"Tom\", \"code\":\"123\"}")
	 * @throws DzcxException 
	 * @Return true 代表发送成功  false 代表发送失败
	*/
    @SuppressWarnings("rawtypes")
	private boolean sendMsg(String phone, String templateCode, String templateParam) throws DzcxException {
        boolean bool = false;
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
        IAcsClient client =new DefaultAcsClient(profile);
        CommonRequest request =new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");// 版本信息  已经固定  不能进行更改
        //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式；发送国际/港澳台消息时，接收号码格式为国际区号+号码，如“85200000000”
        request.putQueryParameter("PhoneNumbers", phone);
        //  阿里云控制台签名
        request.putQueryParameter("SignName", signName);
        // 阿里云控制台模板编号
        request.putQueryParameter("TemplateCode", templateCode);
        request.setAction("SendSms");//系统规定参数
        // 模板内需要填充参数信息
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            logger.info("调用阿里云短信服务请求 phone={}，templateCode={},templateParam={}", phone, templateCode, templateParam);
            CommonResponse response = client.getCommonResponse(request);
            // 下面是一个json格式转换工具，把String 转换为map 也可以转换为对象
            Map map = JsonUtil.fromJson(response.getData(), Map.class);
            logger.info(map.toString());
            if ("OK".equals(map.get("Code"))) {
                bool =true;
            } else {
            	throw new DzcxException(ExceptionCode.短信发送失败, String.valueOf(map.get("Message")));
            }
        }catch (ServerException e) {
            logger.error("阿里云短信服务异常:{}", e);
        }catch (ClientException e) {
            logger.error("连接阿里云短信异常:{}", e);
        }
        return bool;

    }

}
